// approval/pages/depart_approval/depart_approval.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgApproval: '',
    stautsApproval: true,
    readDataShow: false,
    examInfo: {
      approval: false,
      examMemberDetail: '',
      detial: true,
      return: false,
    },
    addQuitExam: {
      quitType: '',
      inductionDate: '',
      quitDate: '',
      memberJob: '',
      jobHandOver: '',
      thingHandOver: '',
      startDate: '',
      endDate: '',
      quitReason: '',
      quitStatus: 0,
      memberName: ''
    },
    listProps: {
      topList: [],
      btmList: [],
      readonlyStatus: false,
    },
    isMusk: false,
    contract_startTime: '', //劳动合同开始时间
    contract_endTime: '', //劳动合同结束时间
    flag: '', //判断是入职还是离职时间
    timeTitle: '', //入职时间离职时间标题
    showTime: false, //入职离职时间
    currentDate: new Date().getTime(), //选择时间
    formatter(type, val) {
      if (type === 'year') {
        return val + '年';
      }
      if (type === 'month') {
        return val + '月';
      }
      if (type === 'day') {
        return val + '日';
      }
      return val;
    },
  },

  // 离职类别
  onRadioChange(e) {
    console.log(e);
    var val = e.detail,
      str = 'addQuitExam.quitType';
    this.setData({
      [str]: val
    })
    console.log(this.data.addQuitExam);
  },

  inputHandle(e) {
    console.log(e);
    var val = e.detail,
      str = e.currentTarget.dataset.str;
    this.setData({
      [str]: val
    })
    console.log(this.data);

  },

  //判断时间遮罩标题
  timeData(e) {
    var flag = e.currentTarget.dataset.flag,
      timeTitle = '',
      showTime = '';
    if (this.data.listProps.readonlyStatus) {
      return false
    }
    if (flag == 1) {
      timeTitle = '入职'
    } else if (flag == 0) {
      timeTitle = '离职'
    } else if (flag == 2) {
      timeTitle = '劳务合同开始'
    } else if (flag == 3) {
      timeTitle = '劳务合同结束'
    }
    showTime = true;
    this.setData({
      flag,
      timeTitle,
      showTime,
      currentDate: new Date().getTime()
    })
  },
  // 选择时间
  resTime(e) {
    var time = new Date(e.detail),
      flag = this.data.flag,
      str = '',
      formartTime = this.formatDate(time);
    console.log(time);
    console.log(formartTime);

    if (flag == 0) {
      str = 'addQuitExam.quitDate'
    } else if (flag == '1') {
      str = 'addQuitExam.inductionDate'
    } else if (flag == 2) {
      str = 'addQuitExam.startDate'
    } else if (flag == 3) {
      str = 'addQuitExam.endDate'
    }
    this.setData({
      [str]: formartTime,
      showTime: false
    })
  },
  formatDate(date) {
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  // 取消选择时间 
  selectCancel() {
    this.setData({
      showTime: false
    })
  },
  // 获取 审批人  抄送人
  getExamProcessDetail() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    var url = app.globalData.url + "exam/getExamProcessDetail",
      that = this,
      listProps = this.data.listProps;
    app.wxRequest(url, {
      examType: 2,
      memberId: app.globalData.memberId
    }, res => {
      console.log(res);
      if (res.flag) {

        var member = app.globalData.member;
        if (member.inCharge == 1) {
          res.data['审批人'].splice(0, 1)
        }
        console.log(member);
        listProps.topList = res.data['审批人'];
        listProps.btmList = res.data['抄送人'];
        console.log(listProps);
        that.setData({
          listProps,
        })
        wx.hideLoading()
      }
    })
  },
  // 组件事件
  change(obj) {
    console.log(obj);
    this.setData({
      listProps: JSON.parse(wx.getStorageSync('listProps'))
    })
    console.log(this.data.listProps);
  },

  // 提交
  toSubmit() {
    var examMemberIdList = [],
      leaveExamTypeList = [],
      listProps = this.data.listProps,
      addQuitExam = this.data.addQuitExam;
    console.log(addQuitExam);
    for (let i in listProps.topList) {
      examMemberIdList.push(listProps.topList[i].memberId);
      leaveExamTypeList.push(0)
    }
    for (let i in listProps.btmList) {
      examMemberIdList.push(listProps.btmList[i].memberId);
      leaveExamTypeList.push(1)
    }
    if (addQuitExam.quitType.length == 0) {
      wx.showToast({
        title: '请选择离职类别',
        icon: 'none'
      })
      return
    }
    if (addQuitExam.inductionDate.length == 0) {
      wx.showToast({
        title: '请选择入职时间',
        icon: 'none'
      })
      return
    }
    if (addQuitExam.quitDate.length == 0) {
      wx.showToast({
        title: '请选择离职时间',
        icon: 'none'
      })
      return
    }
    if (addQuitExam.memberJob.length == 0) {
      wx.showToast({
        title: '请输入岗位',
        icon: 'none'
      })
      return
    }
    if (addQuitExam.jobHandOver.length == 0) {
      wx.showToast({
        title: '请输入交接人',
        icon: 'none'
      })
      return
    }
    if (addQuitExam.thingHandOver.length == 0) {
      wx.showToast({
        title: '请输入交接物品及工作',
        icon: 'none'
      })
      return
    }
    if (addQuitExam.startDate.length == 0) {
      wx.showToast({
        title: '请选择劳务合同开始时间',
        icon: 'none'
      })
      return
    }
    if (addQuitExam.endDate.length == 0) {
      wx.showToast({
        title: '请选择劳务合同结束时间',
        icon: 'none'
      })
      return
    }
    if (addQuitExam.quitReason.length == 0) {
      wx.showToast({
        title: '请输入离职原因',
        icon: 'none'
      })
      return
    }

    if (examMemberIdList.length == 0) {
      wx.showToast({
        title: '审批人为空',
        icon: 'none'
      })
      return
    }
    if (leaveExamTypeList.length == 0) {
      wx.showToast({
        title: '抄送为空',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    var url = app.globalData.url + "exam/addQuitExam",
      that = this;
    app.wxRequest(url, {
      memberId: app.globalData.memberId,
      quitType: addQuitExam.quitType,
      inductionDate: addQuitExam.inductionDate,
      quitDate: addQuitExam.quitDate,
      memberJob: addQuitExam.memberJob,
      jobHandOver: addQuitExam.jobHandOver,
      thingHandOver: addQuitExam.thingHandOver,
      startDate: addQuitExam.startDate,
      endDate: addQuitExam.endDate,
      quitReason: addQuitExam.quitReason,
      quitStatus: 0,
      examType: 2,
      examMemberId: examMemberIdList.toString(),
      examLeaveExamType: leaveExamTypeList.toString(),
    }, res => {
      if (res.flag) {
        wx.hideLoading()
        wx.showToast({
          title: '提交成功',
          iocn: 'none'
        })
        var obj = {
          examMain_id: res.data,
          return: true,
          detial: true,
          stautsApproval: true
        }
        listProps = {
            topList: [],
            btmList: [],
            readonlyStatus: false, // 是否只读
          },
          wx.setStorageSync('examInfo', JSON.stringify(obj))
        that.setData({
          listProps
        })
        that.resultData();
      }
    })
  },
  // 获取 详情
  resultData() {
    var examInfo = JSON.parse(wx.getStorageSync('examInfo')),
      listProps = this.data.listProps;
    listProps.readonlyStatus = true;
    this.setData({
      examInfo,
      listProps
    })
    console.log(this.data.examInfo);
    this.getExamDetail();
    if (this.data.examInfo.examMember_status == 4) {
      this.setData({
        readDataShow: true
      })
    }
    // if (wx.getStorageSync('readDataOa') != '') {
    //   this.setData({
    //     readDataShow: true
    //   })
    // }
    if (examInfo.stautsApproval != undefined) {
      this.setData({
        stautsApproval: false
      })
    }
    if (!examInfo.detial) {
      this.getPenSigner()
    }
    wx.removeStorageSync('examInfo')
    wx.removeStorageSync('readDataOa')
  },

  getExamDetail() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    var url = app.globalData.url + "exam/getExamDetail",
      that = this;
    app.wxRequest(url, {
      examId: that.data.examInfo.examMain_id
    }, res => {
      if (res.flag) {
        var listProps = this.data.listProps,
          addQuitExam = [];

        addQuitExam = res.data.quitExam;
        addQuitExam.memberName = res.data.examMain.member_name;


        for (var i in res.data.examMembers) {
          res.data.examMembers[i].memberName = res.data.examMembers[i].member_name;
          res.data.examMembers[i].memberImg = res.data.examMembers[i].member_img;
          if (res.data.examMembers[i].examMember_type == 0) {
            listProps.topList.push(res.data.examMembers[i])
          } else {
            listProps.btmList.push(res.data.examMembers[i])
          }
        }
        that.setData({
          listProps,
          addQuitExam
        })

        wx.hideLoading()
      }
    })

  },
  // 获取签名版
  getPenSigner() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    console.log(this.data.examInfo);
    var url = app.globalData.url + 'member/getPenSigner',
      that = this;
    app.wxRequest(url, {
      memberId: app.globalData.memberId
    }, res => {
      if (res.flag) {
        if (res.data.penSigners.length == 0) {
          that.setData({
            imgApproval: ''
          })
        } else {
          that.setData({
            imgApproval: res.data.penSigners[0].penSignerImg
          })
        }
      }
    })
  },

  // 撤回
  revokeExam() {
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    console.log(this.data.examInfo);
    var url = app.globalData.url + 'exam/revokeExam',
      that = this;
    app.wxRequest(url, {
      examMainId: that.data.examInfo.examMain_id
    }, res => {
      if (res.flag) {
        console.log(res);
        wx.showModal({
          title: '提示',
          content: '撤回审批成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
              wx.removeStorageSync('examInfo')
            }
          }
        })
        wx.hideLoading()
      }
    })
  },
  // 已阅
  readData() {
    var examMemberNum = '',
      examMemberType = "",
      listProps = this.data.listProps,
      that = this,
      url = app.globalData.url + 'exam/reviewExam';
    for (let i in listProps.btmList) {
      if (listProps.btmList[i].member_id == wx.getStorageSync('member_id')) {
        examMemberNum = listProps.btmList[i].examMember_num;
        examMemberType = listProps.btmList[i].examMember_type;
      }
    }
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    app.wxRequest(url, {
      examMemberId: that.data.examInfo.examMember_id,
      examMemberNum: examMemberNum,
      examMemberDetail: that.data.examInfo.examMemberDetail == undefined ? '' : that.data.examInfo.examMemberDetail,
      examMemberStatus: 1,
      examType: 2,
      examMainId: that.data.examInfo.examMain_id,
      examMemberType: examMemberType
    }, res => {
      if (res.flag) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '审核成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },

  // 同意 或 不同意
  approvalData(e) {
    var flag = e.currentTarget.dataset.flag;
    if (!this.data.examInfo.detial && this.data.imgApproval.length == 0) {
      wx.showModal({
        title: '提示',
        content: '暂无签名板,请完善签名板!',
        complete: (res) => {
          if (res.confirm) {
            console.log('签名版');
            wx.navigateTo({
              url: '/gerenInfo/pages/signature/signature',
            })
          }
        }
      })
      return
    }
    var examMemberNum = '',
      listProps = this.data.listProps,
      url = app.globalData.url + 'exam/reviewExam',
      that = this;
    for (let i in listProps.topList) {
      if (listProps.topList[i].member_id == wx.getStorageSync('member_id')) {
        examMemberNum = listProps.topList[i].examMember_num;
      }
    }
    wx.showLoading({
      title: '',
      icon: 'none'
    })
    app.wxRequest(url, {
      examMemberId: that.data.examInfo.examMember_id,
      examMemberNum: examMemberNum,
      examMemberDetail: that.data.examInfo.examMemberDetail == undefined ? '' : that.data.examInfo.examMemberDetail,
      examMemberStatus: flag,
      examType: 2,
      note: that.data.imgApproval,
      examMainId: that.data.examInfo.examMain_id,
      examMemberType: that.data.examInfo.detial ? 1 : 0
    }, res => {
      if (res.flag) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '审核成功',
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack()
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (wx.getStorageSync('examInfo') != '') {
      this.resultData()
    } else {
      this.getExamProcessDetail();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getPenSigner()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})